﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Student
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
            // 初始化textBox1
            textBox1.Text = "请输入你的帐号";
            textBox1.ForeColor = SystemColors.GrayText;
            textBox1.Enter += textBox1_Enter;
            textBox1.Leave += textBox1_Leave;

            // 初始化textBox2
            textBox2.Text = "请输入你的密码";
            textBox2.ForeColor = SystemColors.GrayText;
            textBox2.Enter += textBox2_Enter;
            textBox2.Leave += textBox2_Leave;
        }

        private void login_button_Click(object sender, EventArgs e)//老师登录页面点击登录后进入的函数
        {
            Models.Teacher teacher = new Models.Teacher();
            teacher.teacher_uname = textBox1.Text;
            teacher.pwd = textBox2.Text;
            


            BLL.B_Teacher b_Teacher = new BLL.B_Teacher();
            bool res = b_Teacher.Login(teacher);
            if (res)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("登录失败，请检查您的用户名和密码是否正确。", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void login_button_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.login_button_Click(sender, e);
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.login_button_Click(sender, e);
            }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            Models.Teacher teacher = new Models.Teacher();
            teacher.teacher_uname = textBox1.Text;
            teacher.pwd = textBox2.Text;



            BLL.B_Teacher b_Teacher = new BLL.B_Teacher();
            bool res = b_Teacher.Login(teacher);
            if (res)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("登录失败，请检查您的用户名和密码是否正确。", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            // 清空学号和密码文本框的内容
            textBox1.Text = "请输入你的帐号";
            textBox1.ForeColor = SystemColors.GrayText; // 恢复灰色前景色
            textBox2.Text = "请输入你的密码";
            textBox2.ForeColor = SystemColors.GrayText; // 恢复灰色前景色
            textBox2.UseSystemPasswordChar = false; // 确保密码字符被隐藏
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            if (textBox1.Text == "请输入你的帐号")
            {
                textBox1.Text = "";
                textBox1.ForeColor = SystemColors.WindowText;
            }
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                textBox1.Text = "请输入你的帐号";
                textBox1.ForeColor = SystemColors.GrayText;
            }
        }

        private void textBox2_Enter(object sender, EventArgs e)
        {
            if (textBox2.Text == "请输入你的密码")
            {
                textBox2.Text = "";
                textBox2.ForeColor = SystemColors.WindowText;
                textBox2.UseSystemPasswordChar = true; // 显示密码字符
            }
        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                textBox2.Text = "请输入你的密码";
                textBox2.ForeColor = SystemColors.GrayText;
                textBox2.UseSystemPasswordChar = false; // 隐藏密码字符
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 切换密码显示状态
            textBox2.UseSystemPasswordChar = !textBox2.UseSystemPasswordChar;
            // 更新按钮图标
            if (textBox2.UseSystemPasswordChar)
            {
                button1.Text = "🙈"; // 隐藏密码时显示闭眼图标
            }
            else
            {
                button1.Text = "👁️"; // 显示密码时显示睁眼图标
            }
        }
    }
}
    

